package nl.moviefan.site.controller;

import java.security.Principal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import nl.moviefan.site.model.ArtikelFilm;
import nl.moviefan.site.model.Customer;
import nl.moviefan.site.service.CustomerService;
import nl.moviefan.site.vestigingen.VestigingConnector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Mapt naar /reservering uri. 
 */
@Controller
@RequestMapping("/reservering")
public class ReserveringController {

    @Autowired
    CustomerService customerService;
        
    /**
     * Methode mapt naar /index uri, haalt alle reserveringen op van gebruiker 
     * en geeft deze weer.
     * @param principal
     * @return 
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView getGereserveerdByCustomer(Principal principal) {
        String currentUserName = principal.getName();
        Customer customer = customerService.getCustomerByUsername(currentUserName);
        int klantnummer = customer.getKlantnummer();
        VestigingConnector vc = new VestigingConnector();
        List<ArtikelFilm> gereserveerdeArtikelen = vc.getGereserveerdeArtikelen(klantnummer);
        ModelAndView mav = new ModelAndView("reservering/index");
        mav.addObject("artikelen", gereserveerdeArtikelen);
        mav.addObject("customer", customer);
        return mav;
    }
    
    /**
     * Methode mapt naar /beschikbaar uri, haalt alle beschikbare artikelen op
     * van vestiging..
     * @param principal
     * @return 
     */
    @RequestMapping(value = "/beschikbaar", method = RequestMethod.GET)
    public ModelAndView getBeschikbaar(Principal principal) {
        String name = principal.getName();
        Customer customer = customerService.getCustomerByUsername(name);
        VestigingConnector vc = new VestigingConnector();
        List<ArtikelFilm> vestigingBeschikbaar = vc.getBeschikbareArtikelen();
        ModelAndView mav = new ModelAndView("reservering/reserveren");
        mav.addObject("artikelen", vestigingBeschikbaar);
        mav.addObject("customer", customer);
        return mav;
    }

    /**
     * Methode mapt naar /reserveren/{artikelnummer} uri, verwerkt reservering.
     * @param artikelnummer
     * @param principal
     * @return 
     */
    @RequestMapping(value = "/reserveren/{artikelnummer}", method = RequestMethod.GET)
    public String maakReservering(@PathVariable("artikelnummer") int artikelnummer, Principal principal) {
        String result = "reservering/fout";
        Customer customer = customerService.getCustomerByUsername(principal.getName());
        int klantnummer = customer.getKlantnummer();
        VestigingConnector vc = new VestigingConnector();
        Calendar calendar = Calendar.getInstance();
        Date now = calendar.getTime();
        Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
        if (vc.makeReservering(artikelnummer, klantnummer, currentTimestamp)) {
            result = "redirect:/reservering/index";
        }
        return result;
    }

}
